home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Lose Your Marbles! 1.0 / source / ls code ƒ / ls main window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  9.5 KB  |  439 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        ls main window.c
  4.  
  5. Purpose:    This module handles initializing/drawing/dealing with the
  6.             main window.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "ls main window.h"
  26. #include "ls meat.h"
  27. #include "ls endgame.h"
  28. #include "ls find.h"
  29. #include "environment.h"
  30. #include "graphics.h"
  31. #include "util.h"
  32. #include "menus.h"
  33. #include "sounds.h"
  34. #include "program globals.h"
  35.  
  36. enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
  37.  
  38. /* internal procedures for ls main window.c only */
  39. static    void SetupTheMainWindow(WindowDataHandle theData);
  40. static    void ShutDownTheMainWindow(WindowDataHandle theData);
  41. static    void InitializeTheMainWindow(WindowDataHandle theData);
  42. static    void OpenTheMainWindow(WindowDataHandle theData);
  43. static    void CloseTheMainWindow(WindowDataHandle theData);
  44. static    void IdleInMainWindow(WindowDataHandle theData);
  45. static    void KeyPressedInMainWindow(WindowDataHandle theData, unsigned char theChar);
  46. static    void MouseClickedInMainWindow(WindowDataHandle theData, Point thePoint);
  47. static    void DisposeTheMainWindow(WindowDataHandle theData);
  48. static    void ActivateTheMainWindow(WindowDataHandle theData);
  49. static    void DeactivateTheMainWindow(WindowDataHandle theData);
  50. static    void DrawTheMainWindow(WindowDataHandle theData, short theDepth);
  51.  
  52. static    short            gOldForegroundTime;        /* stored foreground wait time */
  53. static    short            gDisplayedScore;
  54.  
  55. short MainWindowDispatch(WindowDataHandle theData, short theMessage,
  56.     unsigned long misc)
  57. {
  58.     short            theDepth;
  59.     unsigned char    theChar;
  60.     Point            thePoint;
  61.     GrafPtr            curPort;
  62.     
  63.     switch (theMessage)
  64.     {
  65.         case kNull:
  66.             IdleInMainWindow(theData);
  67.             return kSuccess;
  68.             break;
  69.         case kUpdate:
  70.             theDepth=misc&0x7fff;
  71.             DrawTheMainWindow(theData, theDepth);
  72.             return kSuccess;
  73.             break;
  74.         case kInitialize:
  75.             InitializeTheMainWindow(theData);
  76.             return kFailure;
  77.         case kOpen:
  78.             OpenTheMainWindow(theData);
  79.             return kSuccess;
  80.             break;
  81.         case kClose:
  82.             CloseTheMainWindow(theData);
  83.             return kSuccess;
  84.             break;
  85.         case kKeydown:
  86.             theChar=misc&charCodeMask;
  87.             KeyPressedInMainWindow(theData, theChar);
  88.             return kSuccess;
  89.             break;
  90.         case kMousedown:
  91.              thePoint.h=(misc>>16)&0x7fff;
  92.              thePoint.v=misc&0x7fff;
  93.              MouseClickedInMainWindow(theData, thePoint);
  94.              return kSuccess;
  95.              break;
  96.          case kActivate:
  97.              ActivateTheMainWindow(theData);
  98.              return kSuccess;
  99.              break;
  100.          case kDeactivate:
  101.              DeactivateTheMainWindow(theData);
  102.              return kSuccess;
  103.              break;
  104.         case kStartup:
  105.             SetupTheMainWindow(theData);
  106.             return kSuccess;
  107.             break;
  108.         case kDispose:
  109.             DisposeTheMainWindow(theData);
  110.             return kSuccess;
  111.             break;
  112.         case kShutdown:
  113.             ShutDownTheMainWindow(theData);
  114.             return kSuccess;
  115.             break;
  116.         case kUndo:
  117.             if (!ShowingEndGameQQ())
  118.             {
  119.                 if (UndoOneMove(theData))
  120.                     DoSound(sound_undo, TRUE);
  121.                 else
  122.                     DebugStr("\pUndoOneMove failed");
  123.             }
  124.             return kSuccess;
  125.             break;
  126.     }
  127.     
  128.     return kFailure;
  129. }
  130.  
  131. void SetupTheMainWindow(WindowDataHandle theData)
  132. {
  133.     unsigned char    *titleStr="\pLose Your Marbles!";
  134.     short            i;
  135.     
  136.     (**theData).maxDepth=8;
  137.     (**theData).windowType=noGrowDocProc;
  138.     (**theData).hasCloseBox=TRUE;
  139.     Mymemcpy((Ptr)((**theData).windowTitle), (Ptr)titleStr, titleStr[0]+1);
  140.     
  141.     for (i=0; i<10; i++)
  142.     {
  143.         if (gHasColorQD)
  144.             gTheCIcon[i]=GetCIcon(i+128);
  145.         else
  146.             gTheIcon[i]=GetIcon(i+128);
  147.     }
  148. }
  149.  
  150. void ShutDownTheMainWindow(WindowDataHandle theData)
  151. {
  152.     short            i;
  153.     
  154.     for (i=0; i<10; i++)
  155.     {
  156.         if (gHasColorQD)
  157.             DisposeCIcon(gTheCIcon[i]);
  158.         else
  159.             ReleaseResource(gTheIcon[i]);
  160.     }
  161. }
  162.  
  163. void InitializeTheMainWindow(WindowDataHandle theData)
  164. {
  165.     (**theData).windowWidth=gNumColumns*38+2;
  166.     (**theData).windowHeight=gNumRows*38+2;
  167.     DoSound(sound_startgame, TRUE);
  168. }
  169.  
  170. void OpenTheMainWindow(WindowDataHandle theData)
  171. {
  172.     (**theData).offscreenNeedsUpdate=TRUE;
  173. }
  174.  
  175. void CloseTheMainWindow(WindowDataHandle theData)
  176. {
  177. }
  178.  
  179. void IdleInMainWindow(WindowDataHandle theData)
  180. {
  181. }
  182.  
  183. void KeyPressedInMainWindow(WindowDataHandle theData, unsigned char theChar)
  184. {
  185.     short            theValue;
  186.     Rect            tempRect;
  187.     Point            currentPoint;
  188.     
  189.     ObscureCursor();
  190.     
  191.     if (ShowingEndGameQQ())
  192.     {
  193.         DontShowEndGame(theData);
  194.         return;
  195.     }
  196.     
  197.     if (ShowingFindQQ())
  198.     {
  199.         DontShowFind(theData);
  200.         return;
  201.     }
  202.     
  203.     if (GameOverQQ())
  204.         return;
  205.     
  206.     theValue=-1;
  207.     switch (theChar)
  208.     {
  209.         case '0':
  210.             theValue=10;
  211.             break;
  212.         case '1':
  213.         case '2':
  214.         case '3':
  215.         case '4':
  216.         case '5':
  217.         case '6':
  218.         case '7':
  219.         case '8':
  220.         case '9':
  221.             theValue=theChar-'0';
  222.             break;
  223.         case ' ':
  224.         case 0x08:
  225.             theValue=0;
  226.             break;
  227.         case 'j':
  228.         case 'J':
  229.         case key_LeftArrow:
  230.             FramePosition(gCurrentRow, gCurrentColumn, FALSE);
  231.             gCurrentColumn--;
  232.             if (gCurrentColumn<0)
  233.                 gCurrentColumn=gNumColumns-1;
  234.             FramePosition(gCurrentRow, gCurrentColumn, TRUE);
  235.             (**theData).offscreenNeedsUpdate=TRUE;
  236.             break;
  237.         case 'i':
  238.         case 'I':
  239.         case key_UpArrow:
  240.             FramePosition(gCurrentRow, gCurrentColumn, FALSE);
  241.             gCurrentRow--;
  242.             if (gCurrentRow<0)
  243.                 gCurrentRow=gNumRows-1;
  244.             FramePosition(gCurrentRow, gCurrentColumn, TRUE);
  245.             (**theData).offscreenNeedsUpdate=TRUE;
  246.             break;
  247.         case 'k':
  248.         case 'K':
  249.         case key_RightArrow:
  250.             FramePosition(gCurrentRow, gCurrentColumn, FALSE);
  251.             gCurrentColumn++;
  252.             if (gCurrentColumn>=gNumColumns)
  253.                 gCurrentColumn=0;
  254.             FramePosition(gCurrentRow, gCurrentColumn, TRUE);
  255.             (**theData).offscreenNeedsUpdate=TRUE;
  256.             break;
  257.         case 'm':
  258.         case 'M':
  259.         case key_DownArrow:
  260.             FramePosition(gCurrentRow, gCurrentColumn, FALSE);
  261.             gCurrentRow++;
  262.             if (gCurrentRow>=gNumRows)
  263.                 gCurrentRow=0;
  264.             FramePosition(gCurrentRow, gCurrentColumn, TRUE);
  265.             (**theData).offscreenNeedsUpdate=TRUE;
  266.             break;
  267.     }
  268.     
  269.     if (theValue!=-1)
  270.     {
  271.         if (Board[gCurrentRow][gCurrentColumn]!=theValue)
  272.         {
  273.             currentPoint.h=gCurrentColumn;
  274.             currentPoint.v=gCurrentRow;
  275.             
  276.             if (theValue!=0)
  277.                 DoSound(sound_click, TRUE);
  278.             else
  279.                 DoSound(sound_undo, TRUE);
  280.             
  281.             if (Board[gCurrentRow][gCurrentColumn]!=0)
  282.                 if (!DeleteOneMove(currentPoint))
  283.                     DebugStr("\pDeleteOneMove failed");
  284.             if (theValue!=0)
  285.                 if (!AddOneMove(currentPoint))
  286.                     DebugStr("\pAddOneMove failed");
  287.             
  288.             Board[gCurrentRow][gCurrentColumn]=theValue;
  289.             DrawOnePosition(gCurrentRow, gCurrentColumn, Board[gCurrentRow][gCurrentColumn], FALSE);
  290.             
  291.             (**theData).offscreenNeedsUpdate=TRUE;
  292.         }
  293.     }
  294.     
  295.     CheckEndGame(theData);
  296. }
  297.  
  298. void MouseClickedInMainWindow(WindowDataHandle theData, Point thePoint)
  299. {
  300.     short            theRow, theColumn;
  301.     short            clickRow, clickColumn;
  302.     Boolean            gotone;
  303.     Rect            theRect;
  304.     
  305.     if (ShowingEndGameQQ())
  306.     {
  307.         DontShowEndGame(theData);
  308.         return;
  309.     }
  310.     
  311.     if (ShowingFindQQ())
  312.     {
  313.         DontShowFind(theData);
  314.         return;
  315.     }
  316.     
  317.     if (GameOverQQ())
  318.         return;
  319.     
  320.     theRect.left=theRect.top=2;
  321.     theRect.bottom=theRect.right=36;
  322.     gotone=FALSE;
  323.     for (theRow=0; ((theRow<gNumRows) && (!gotone)); theRow++)
  324.     {
  325.         for (theColumn=0; ((theColumn<gNumColumns) && (!gotone)); theColumn++)
  326.         {
  327.             if (PtInRect(thePoint, &theRect))
  328.             {
  329.                 gotone=TRUE;
  330.                 clickRow=theColumn;
  331.                 clickColumn=theRow;
  332.             }
  333.             
  334.             theRect.top+=38;
  335.             theRect.bottom+=38;
  336.         }
  337.         
  338.         theRect.left+=38;
  339.         theRect.right+=38;
  340.         theRect.top=2;
  341.         theRect.bottom=38;
  342.     }
  343.     
  344.     if (gotone)
  345.     {
  346.         if ((gCurrentRow!=clickRow) || (gCurrentColumn!=clickColumn))
  347.         {
  348.             FramePosition(gCurrentRow, gCurrentColumn, FALSE);
  349.             FramePosition(clickRow, clickColumn, TRUE);
  350.             
  351.             gCurrentRow=clickRow;
  352.             gCurrentColumn=clickColumn;
  353.         }
  354.     }
  355. }
  356.  
  357. void DisposeTheMainWindow(WindowDataHandle theData)
  358. {
  359.     AdjustMenus();
  360.     DrawMenuBar();
  361. }
  362.  
  363. void ActivateTheMainWindow(WindowDataHandle theData)
  364. {
  365.     gOldForegroundTime=gForegroundWaitTime;
  366.     gForegroundWaitTime=0;
  367. }
  368.  
  369. void DeactivateTheMainWindow(WindowDataHandle theData)
  370. {
  371.     gForegroundWaitTime=gOldForegroundTime;
  372. }
  373.  
  374. void DrawTheMainWindow(WindowDataHandle theData, short theDepth)
  375. {
  376.     RGBColor        oldForeColor, oldBackColor;
  377.     GrafPtr            curPort;
  378.     short            i,j;
  379.     Str31            theStr;
  380.     Rect            tempRect;
  381.     short            a, b, c, d;
  382.     
  383.     if (theDepth>2)
  384.     {
  385.         GetForeColor(&oldForeColor);
  386.         GetBackColor(&oldBackColor);
  387.     }
  388.     
  389.     GetPort(&curPort);
  390.     EraseRect(&(curPort->portRect));
  391.     
  392.     if (ShowingEndGameQQ())
  393.     {
  394.         DrawEndGame(theData, theDepth);
  395.     }
  396.     else
  397.     {
  398.         for (i=0; i<gNumRows; i++)
  399.         {
  400.             for (j=0; j<gNumColumns; j++)
  401.             {
  402.                 tempRect.top=i*38+2;
  403.                 tempRect.bottom=tempRect.top+36;
  404.                 tempRect.left=j*38+2;
  405.                 tempRect.right=tempRect.left+36;
  406.                 FrameRect(&tempRect);
  407.                 
  408.                 if ((gCurrentRow==i) && (gCurrentColumn==j) && (!ShowingFindQQ()) &&
  409.                     (!GameOverQQ()))
  410.                 {
  411.                     FramePosition(i, j, TRUE);
  412.                 }
  413.                 
  414.                 if (Board[i][j]>0)
  415.                 {
  416.                     InsetRect(&tempRect, 2, 2);
  417.                     if (gHasColorQD)
  418.                         PlotCIcon(&tempRect, gTheCIcon[Board[i][j]-1]);
  419.                     else
  420.                         PlotIcon(&tempRect, gTheIcon[Board[i][j]-1]);
  421.                 }
  422.             }
  423.         }
  424.         
  425.         if (ShowingFindQQ())
  426.         {
  427.             GetProblemPositions(&a, &b, &c, &d);
  428.             DrawOnePosition(a, b, Board[a][b], TRUE);
  429.             DrawOnePosition(c, d, Board[c][d], TRUE);
  430.         }
  431.     }
  432.     
  433.     if (theDepth>2)
  434.     {
  435.         RGBForeColor(&oldForeColor);
  436.         RGBBackColor(&oldBackColor);
  437.     }
  438. }
  439.